home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DBio / DSeqDoc.cpp < prev    next >
Text File  |  1996-07-05  |  40KB  |  1,510 lines

  1. // DSeqDoc.cp
  2.  
  3. #define ETEXT 1
  4.  
  5.  
  6. #include "DSeqDoc.h"
  7. #include <ncbi.h>
  8. #include <dgg.h>
  9. #include <Dvibrant.h>
  10. #include <DApplication.h>
  11. #include <DClipboard.h>
  12. #include <DControl.h>
  13. #include <DWindow.h>
  14. #include <DRichViewNu.h>
  15. #include <DTableView.h>
  16. #include <DViewCentral.h>
  17. #include <DTask.h>
  18. #include <DTracker.h>
  19. #include <DMenu.h>
  20. #include <DUtil.h>
  21. #include <DFindDlog.h>
  22. #include "DRichHandler.h"
  23. #include "DSeqFile.h"
  24. #include "DSeqEd.h"
  25. #include "DSeqMail.h"
  26. #include "DSeqCmds.h"
  27. #include "DSeqChildApp.h"
  28. #include "DSeqPrint.h"
  29. #include "DSeqPict.h"
  30. #include "DSeqViews.h"
  31.  
  32. //#include "DSeqAsmView.h"
  33.  
  34.  
  35.  
  36. Global char* gDefSeqName = NULL;
  37. static char gDefSeqNameStore[128];
  38. Boolean  DSeqDoc::fgTestSeqFile = false; // turn on/off seq-test for OpenDocument()
  39. Boolean  DSeqDoc::fgStartDoc = true;
  40. short         DSeqDoc::fgMatKind= 0;
  41. short         DSeqDoc::fgDistCor= 0;
  42. Boolean  DSeqDoc::fgLockText = false;  
  43. short         DSeqDoc::fgViewMode = DAlnView::kModeSlide;
  44. //Boolean  DSeqDoc::fgUseColor = false;  
  45. short         DSeqDoc::fgUseColor = DAlnView::kBaseColor;
  46. Nlm_RecT DSeqDoc::fgWinRect = { 0, 0, 0, 0 };
  47. char *     DSeqDoc::fgSire = "SPup";
  48.  
  49.  
  50.  
  51.  
  52. class DSeqFormatPopup : public DPopupList
  53. {
  54. public:
  55.     DSeqFormatPopup(long id, DView* superior, short defaultFormat);
  56.     virtual short GetValue() { return DSeqFile::kMinFormat + DPopupList::GetValue() - 1; }
  57. };
  58.  
  59. DSeqFormatPopup::DSeqFormatPopup(long id, DView* superior, short defaultFormat) :
  60.             DPopupList(id,superior,true)
  61. {
  62.     for (short i= DSeqFile::kMinFormat; i<=DSeqFile::kMaxFormat; i++) 
  63.         this->AddItem( (char*)DSeqFile::FormatName(i));
  64.     this->SetValue(defaultFormat);
  65. }
  66.  
  67.  
  68.  
  69. class DAlnModePopup : public DPopupList
  70. {
  71. public:
  72.     DAlnModePopup(long id, DView* superior, short defaultvalue);
  73.     virtual short GetValue() { return DAlnView::kModeSlide + DPopupList::GetValue() - 1; }
  74. };
  75.  
  76. DAlnModePopup::DAlnModePopup(long id, DView* superior, short defaultvalue) :
  77.             DPopupList(id,superior,true)
  78. {
  79.     AddItem( "Slide bases");
  80.     AddItem( "Edit bases");
  81.     AddItem( "Select mask 1");
  82.     AddItem( "Select mask 2");
  83.     AddItem( "Select mask 3");
  84.     AddItem( "Select mask 4");
  85.     this->SetValue(defaultvalue+1);
  86. }
  87.  
  88.  
  89. class DAlnColorPopup : public DPopupList
  90. {
  91. public:
  92.     DAlnColorPopup(long id, DView* superior, short defaultvalue);
  93.     virtual short GetValue() { return DAlnView::kBaseBlack + DPopupList::GetValue() - 1; }
  94. };
  95.  
  96. DAlnColorPopup::DAlnColorPopup(long id, DView* superior, short defaultvalue) :
  97.             DPopupList(id,superior,true)
  98. {
  99.     AddItem( "Black");
  100.     AddItem( "Color");
  101.     AddItem( "Align");
  102.     AddItem( "Back color");
  103.     AddItem( "Back align");
  104.     this->SetValue(defaultvalue +1 - DAlnView::kBaseBlack);
  105. }
  106.  
  107.  
  108.  
  109. // class DAlnFindDialog
  110.  
  111. class DAlnFindDialog : public DFindDialog {
  112. public:
  113.     DSequence* fSequence;
  114.     DAlnView* fAlnView;
  115.     short     fRow;
  116.     char    * fLastTarget;
  117.     long        fLastMatch;
  118.     DAlnFindDialog( DSequence* itsSeq, DAlnView* itsView);
  119.     virtual void DoFind();
  120.     virtual void BuildDlog();
  121. };
  122.  
  123. DAlnFindDialog::DAlnFindDialog( DSequence* itsSeq, DAlnView* itsView) :
  124.         fSequence(itsSeq), fAlnView( itsView), 
  125.         fLastTarget(NULL), fRow(-1), fLastMatch(-1)
  126. {
  127. }
  128.  
  129. void DAlnFindDialog::BuildDlog()
  130. {
  131.     DFindDialog::BuildDlog();
  132.  
  133.     DView* av;
  134.     av= FindSubview(replaceId); if (av) { av->Disable(); } //av->Hide();
  135.     av= FindSubview(replaceFindId); if (av) { av->Disable();  }
  136.     av= FindSubview(replaceAllId); if (av) { av->Disable(); }
  137.     av= FindSubview(cBackwards);     if (av) { av->Disable(); }
  138.     av= FindSubview(cFullWord); if (av) { av->Disable(); }
  139.     av= FindSubview(cCaseSense); if (av) { av->Disable(); }
  140.     if (fReplaceText) fReplaceText->Disable();
  141. }
  142.  
  143. void DAlnFindDialog::DoFind() 
  144. {
  145.     long    firstBase, nBases, match;
  146.     const char    *target;
  147.     Boolean back;
  148.     Nlm_RecT selr;
  149.     long        selrow;
  150.     
  151.     //fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  152.     fSequence= fAlnView->SelectedSequence(selrow);
  153.     fRow= selrow;
  154.     if (fSequence) {
  155.         fSequence->UpdateFlds();  
  156.         selr= fAlnView->GetSelRect();
  157.         selr.top= fRow;
  158.         selr.bottom= fRow+1; //??
  159.         firstBase= selr.left;
  160.         nBases= 0; //selr.right - firstBase;
  161.         fSequence->SetSelection( firstBase, nBases);
  162.         target= this->GetFind();
  163.         back    = this->Backwards();
  164.         if (target && *target) {
  165.             if (fLastTarget && (StringCmp(target, fLastTarget)==0) && firstBase == fLastMatch)
  166.                 match= fSequence->SearchAgain();
  167.             else {
  168.                 if (fLastTarget) MemFree(fLastTarget);
  169.                 fLastTarget= StrDup(target);
  170.                 match= fSequence->Search( (char*)target, back);
  171.                 }
  172.             fLastMatch= match;
  173.             if (match>=0) {
  174.                 nBases= StrLen(target);
  175.                 fSequence->SetSelection( match, nBases);
  176.                 selr.left= match;
  177.                 selr.right= match + nBases;
  178.                 fAlnView->SelectCells( selr);  
  179.                 if (fAlnView->fEditSeq && fAlnView->fEditRow == selrow)
  180.                     fAlnView->fEditSeq->Select(match, nBases);
  181.                 }
  182.             }
  183.         }
  184. }                
  185.  
  186.  
  187.  
  188. //class DSeqDoc
  189.  
  190.  
  191. DSeqDoc::DSeqDoc( long id, DSeqList* itsSeqList, char* name) :
  192.         DWindow( id, gApplication),
  193.         fAlnView(NULL), fAlnIndex(NULL), fAlnITitle(NULL),
  194.         fSeqList(itsSeqList), fPrintDoc(NULL),
  195.         fHeadline(NULL), fSeqMeter(NULL), fFormatPop(NULL),
  196.         fUpdateTime(0), fDocTitle(NULL),
  197.         fInFormat(DSeqFile::kGenBank), // kNoFormat
  198.         fSaveSelection(false),
  199.         //fLockButton(NULL), fColorButton(NULL), fMonoButton(NULL),
  200.         fColorCheck(NULL), fLockCheck(NULL),
  201.         fUseColor(false)
  202.     short width= -1, height= -1, left= -10, top= -20; // default window loc
  203.     if (gTextFont == NULL) gTextFont= Nlm_programFont;
  204.     if (gSeqFont == NULL) gSeqFont= Nlm_programFont;
  205.  
  206.     if (!Nlm_EmptyRect(&fgWinRect))  {
  207.             // these two are causing resize to fail >> width & height setting !!
  208.         //width= MAX( 40, fgWinRect.right - fgWinRect.left);
  209.         //height= MAX( 50, fgWinRect.bottom - fgWinRect.top);
  210.         left= MAX(20,fgWinRect.left);
  211.         top = MAX(40,fgWinRect.top);
  212.         }
  213.  
  214.     this->InitWindow( document, width, height, left, top, name); 
  215.     gDefSeqName= name; 
  216.     
  217.     if (!fSeqList) fSeqList= new DSeqList();  
  218.     //fSeqList->AddNewSeq();    // install 1 blank seq for new doc
  219. }
  220.  
  221.  
  222. DSeqDoc::~DSeqDoc()
  223. {
  224.     fAlnView->DeInstallEditSeq(); 
  225.     if (fSeqList) { 
  226.         //fSeqList->FreeAllObjects(); 
  227.         //fSeqList->suicide(); // need this for other users of list ??
  228.         fSeqList->fDeleteObjects= true; 
  229.         delete fSeqList; // knows how to delete
  230.         }
  231. }
  232.  
  233. void DSeqDoc::FreeData()
  234. {
  235.     fAlnView->DeInstallEditSeq(); 
  236.     if (fSeqList) { 
  237.         //fSeqList->FreeAllObjects(); // DaMN !! THIS IS NOT CALLING Seq Destructors !!
  238.         long i, n= fSeqList->GetSize();
  239.         for (i=0; i<n; i++) {
  240.             DSequence* aseq= fSeqList->SeqAt(i);
  241.             delete aseq;
  242.             }
  243.         fSeqList->DeleteAll();
  244.         }
  245. }
  246.  
  247.  
  248. Boolean DSeqDoc::IsSeqFile(DFile* aFile)
  249. {
  250.     // this is a static function, usable w/o a DGopherListDoc object
  251.     if (fgTestSeqFile && aFile && aFile->Exists()) {
  252.         aFile->OpenFile();
  253.         short format= DSeqFile::SeqFileFormatWrapper( aFile);
  254.         return (format != DSeqFile::kUnknown);
  255.         }
  256.     else
  257.         return false;
  258. }
  259.  
  260. void DSeqDoc::MakeGlobalsCurrent()
  261. {
  262.     ViewRect( fViewrect); // get current rect...
  263.     if (!Nlm_EmptyRect(&fViewrect)) fgWinRect= fViewrect;  
  264.     if (fLockCheck) fgLockText= fLockCheck->GetStatus();
  265.     //if (fColorCheck) fgUseColor= fColorCheck->GetStatus();
  266.     if (fColorPop) fgUseColor= fColorPop->GetValue();
  267.     if (fModePop) fgViewMode= fModePop->GetValue();
  268. }
  269.  
  270. void DSeqDoc::Close()
  271. {
  272. #if 1
  273.     DList* winlist= gWindowManager->GetWindowList();
  274.     short  i, nwin= winlist->GetSize();
  275.     for (i=0; i<nwin; i++) {
  276.         DWindow* awin= (DWindow*) winlist->At(i);
  277.         if (awin->Id() == DSeqedWindow::kId 
  278.             && ((DSeqedWindow*) awin)->fMainDoc == this) { 
  279.                 delete awin; // ?? or awin->Close();
  280.                  }
  281.         }
  282. #endif
  283.     
  284.     MakeGlobalsCurrent();
  285.     DWindow::Close();
  286. }
  287.  
  288. void DSeqDoc::ResizeWin()
  289. {
  290.     DWindow::ResizeWin();
  291.     MakeGlobalsCurrent();
  292. }
  293.  
  294. void DSeqDoc::Open(DFile* aFile)
  295. {
  296.     Boolean isempty= (fSeqList->GetSize() < 1);
  297.     if ( ReadFrom( aFile, true) ) {
  298.         if (isempty) {
  299.             this->SetTitle((char*)aFile->GetShortname());
  300.             this->SetFilename((char*)aFile->GetName());
  301.             }
  302.         this->Open();
  303.         }
  304.     else 
  305.         if (isempty) this->suicide();
  306. }
  307.  
  308.  
  309. void DSeqDoc::Revert()
  310. {
  311.     char buf[512];
  312.     char *name= GetFilename(buf, sizeof(buf));
  313.     DFile aFile(name);
  314.     fAlnView->DeInstallEditSeq(); 
  315.     if ( ReadFrom( &aFile, false) ) {
  316.         //this->Open();
  317.         DSaveHandler::NotDirty();
  318.         fAlnView->GetReadyToShow();  
  319.         fAlnView->Invalidate();
  320.         fAlnITitle->GetReadyToShow();
  321.         if (fAlnIndex){
  322.             fAlnIndex->GetReadyToShow();
  323.             fAlnIndex->Invalidate();
  324.             }
  325.         }
  326. }
  327.  
  328.  
  329. // static
  330. void DSeqDoc::NewSeqDoc()
  331. {
  332.     DSeqDoc* newdoc= new DSeqDoc( DSeqDoc::kSeqdoc, NULL, (char*)DFileManager::kUntitled);
  333.     newdoc->Open();
  334.     newdoc->EditSeqs(); //?? make it real obvious to beginners?
  335. }
  336.  
  337. // static
  338. void DSeqDoc::GetGlobals()
  339. {
  340.     char* onoffs;
  341.  
  342.     SeqDocPrefs(kSeqDocPrefInit);
  343.     DSeqPrintDoc::GetGlobals();
  344.     SeqPrintPrefs(kSeqPrintPrefInit);
  345.  
  346.     onoffs= (fgLockText) ? "1" : "0";
  347.     fgLockText= gApplication->GetPrefVal( "fgLockText", "windows", onoffs);
  348.     //onoffs= (fgUseColor) ? "1" : "0";
  349.     //fgUseColor= gApplication->GetPrefVal( "fgUseColor", "windows", onoffs);
  350.     fgUseColor= gApplication->GetPrefVal( "fgUseColor", "windows", "0");
  351.     fgViewMode= gApplication->GetPrefVal( "fgViewMode", "seqdoc", "0");
  352.     fgSire= gApplication->GetPref( "fgSire", "seqdoc", "SApp");
  353.     //DSeqFile::fgSire= fgSire; // messy !
  354.     //DSeqList::fgSire= fgSire; // messy !
  355.     DFile::fgSire= fgSire; // messy !
  356.     
  357.     {
  358.     //350, 200
  359.     char* srect = gApplication->GetPref( "fgWinRect", "windows", "30 40 450 220");
  360. #if 1
  361.         // sscanf is failing on Mac/codewar !! used to work
  362.     if (srect) {
  363.         char* cp= srect;
  364.         while (*cp && isspace(*cp)) cp++;
  365.         fgWinRect.left= atoi( cp);
  366.         
  367.         while (*cp && !isspace(*cp)) cp++;
  368.         while (*cp && isspace(*cp)) cp++;
  369.         fgWinRect.top= atoi( cp);
  370.         
  371.         while (*cp && !isspace(*cp)) cp++;
  372.         while (*cp && isspace(*cp)) cp++;
  373.         fgWinRect.right= atoi( cp);
  374.         
  375.         while (*cp && !isspace(*cp)) cp++;
  376.         while (*cp && isspace(*cp)) cp++;
  377.         fgWinRect.bottom= atoi( cp);
  378.         }
  379. #else
  380.         if (srect) sscanf( srect, "%d%d%d%d", &fgWinRect.left, &fgWinRect.top, 
  381.                                                 &fgWinRect.right, &fgWinRect.bottom);
  382. #endif
  383.         MemFree(srect);
  384.     }
  385. }
  386.  
  387. // static
  388. void DSeqDoc::SaveGlobals()
  389. {
  390.     gApplication->SetPref( (int) fgLockText,"fgLockText","windows");
  391.     gApplication->SetPref( (int) fgUseColor, "fgUseColor","windows");
  392.     gApplication->SetPref( (int) fgViewMode, "fgViewMode","seqdoc");
  393.     gApplication->SetPref( fgSire, "fgSire","seqdoc");
  394.  
  395.     if (!Nlm_EmptyRect(&fgWinRect)) {
  396.         char  srect[128];
  397.         sprintf( srect, "%d %d %d %d", fgWinRect.left, fgWinRect.top, 
  398.                                                         fgWinRect.right, fgWinRect.bottom);
  399.         gApplication->SetPref( srect, "fgWinRect", "windows");
  400.         }
  401. }
  402.  
  403.  
  404. void DSeqDoc::AddAlnIndex(DView* super, short width, short height)
  405. {
  406.     fAlnIndex= new DAlnIndex(0, super, this, fSeqList, width, height); 
  407. }
  408.  
  409. void DSeqDoc::AddAlnView(DView* super, short width, short height)
  410. {
  411.     fAlnView= new DAlnView(0, super, this, fSeqList, width, height); 
  412. }
  413.  
  414. void DSeqDoc::AddModePopup(DView* super)
  415. {
  416.     fModePop= new DAlnModePopup(kModePopup, super, fgViewMode);
  417. }
  418.  
  419. void DSeqDoc::AddTopViews(DView* super)
  420. {
  421.     DPrompt* pr;
  422.  
  423.     pr= new DPrompt(0,super,"Color:",0, 0, Nlm_programFont);
  424.     super->NextSubviewToRight();
  425.     fColorPop= new DAlnColorPopup(kColorPopup,super,fgUseColor);
  426.     fUseColor= fgUseColor;
  427.     super->NextSubviewToRight();
  428.  
  429.     pr= new DPrompt(0,super," View:",0, 0, Nlm_programFont);
  430.                     
  431.     super->NextSubviewToRight();
  432.     this->AddModePopup(super); 
  433.     fLockCheck= NULL;
  434.  
  435.     super->NextSubviewToRight();
  436.     pr= new DPrompt(0,super,"  File format:",0, 0, Nlm_programFont);
  437.  
  438.     DCluster* metergrp= new DCluster( 0, this, 0, 0, true);   // was 0,0 for w,h
  439.     pr= new DPrompt(0,metergrp,"Base#",0, 0, Nlm_programFont);
  440.     metergrp->NextSubviewToRight();
  441.     fSeqMeter= new DPrompt(0,metergrp,"0000000",0,0,Nlm_programFont,justright); //60,14
  442.  
  443.     super->NextSubviewToRight();
  444.     fFormatPop= new DSeqFormatPopup(0,super,fInFormat);
  445.     super->NextSubviewBelowLeft();
  446. }
  447.  
  448.  
  449. void DSeqDoc::AddViews()
  450. {
  451.     DView* super;
  452.     short width, height;
  453.     Nlm_PoinT        nps;
  454.     DPrompt* pr;
  455.     
  456.     if (!fAlnView || !fAlnIndex || !fAlnHIndex || !fAlnITitle) {
  457.         super= this;
  458.         
  459.         AddTopViews( super);
  460.  
  461.             // left group : titleline over name table
  462.         DCluster* grouper= new DCluster( 0, this, 0, 0, true);   // was 0,0 for w,h
  463.         grouper->SetResize( DView::fixed, DView::relsuper);
  464.         super= grouper;
  465.  
  466.         fAlnITitle= new DAlnITitle(0, super, 80, Nlm_stdLineHeight);
  467.         super->NextSubviewBelowLeft();
  468.  
  469.         //width= 350; height= 200;
  470.         super->GetNextPosition( &nps);
  471.         width = MAX( 40, fgWinRect.right - fgWinRect.left) - Nlm_vScrollBarWidth  - nps.x; 
  472.         height= MAX( 60, fgWinRect.bottom - fgWinRect.top) - Nlm_hScrollBarHeight - nps.y;   
  473.         
  474.         this->AddAlnIndex( super, 80, height);
  475.  
  476.         super= this;
  477.         super->NextSubviewToRight();    
  478.      
  479.         super->GetNextPosition( &nps);
  480.         nps.x += 3;
  481.         super->SetNextPosition( nps);
  482.  
  483.             // right group : index topline over bases table
  484.         grouper= new DCluster( 0, this, 0, 0, true); // was 0,0 wid,hgt
  485.         grouper->SetResize( DView::relsuper, DView::relsuper);
  486.         super= grouper;
  487.         super->GetNextPosition( &nps);
  488.         width = MAX( 40, fgWinRect.right - fgWinRect.left) - Nlm_vScrollBarWidth  - nps.x; 
  489.         height= MAX( 60, fgWinRect.bottom - fgWinRect.top) - Nlm_hScrollBarHeight - nps.y; 
  490.         fAlnHIndex= new DAlnHIndex(0, super, this, fSeqList, width, Nlm_stdLineHeight);
  491.         super->NextSubviewBelowLeft();
  492.         
  493.         //width= 350; height= 200;
  494.         super->GetNextPosition( &nps);
  495.         width = MAX( 40, fgWinRect.right - fgWinRect.left) - Nlm_vScrollBarWidth  - nps.x; 
  496.         height= MAX( 60, fgWinRect.bottom - fgWinRect.top) - Nlm_hScrollBarHeight - nps.y; 
  497.         this->AddAlnView( super, width, height);
  498.         fAlnView->SetTextLock( fgLockText);
  499.         super= this;
  500.  
  501. #if ETEXT
  502.         //if (gLastEditView) gLastEditView->DeInstallEditSeq();
  503.         {
  504.         short charwidth= 0;
  505.     Nlm_PoinT  nps, saveps;
  506.         GetNextPosition( &saveps);
  507.         nps.x= 1; nps.y= 1; // locate dialog item 
  508.         SetNextPosition( nps);
  509.         //gDialogTextMultiline= false;
  510.  
  511.         charwidth= width / fAlnView->GetItemWidth();
  512.  
  513.         DAlnSequence* eseq= new DAlnSequence( 0, fAlnView, charwidth);  
  514.         fAlnView->fEditSeq= eseq;
  515.         //this->SetEditText(eseq);
  516.         eseq->fVisible= true;
  517.         eseq->HideEdit(); // eseq->Disable(); eseq->Hide();
  518.         this->SetEditText(NULL);  
  519.         SetNextPosition( saveps);
  520.         }
  521. #endif
  522.         }
  523.         
  524.     if (!fFindDlog) fFindDlog= new DAlnFindDialog( NULL, (DAlnView*)fAlnView);
  525. }
  526.  
  527.  
  528.  
  529. void DSeqDoc::Open()
  530. {
  531.     Boolean selectone= false;
  532.     
  533.     if (!fSeqList) fSeqList= new DSeqList();
  534.     if (fSeqList->GetSize() == 0) {
  535.         fSeqList->AddNewSeq();    // install 1 blank seq for new doc
  536.         selectone= true;
  537.         }
  538.     
  539.     AddViews();
  540.     fSaveHandler= this;  
  541.     fPrintHandler= this; 
  542.  
  543.     this->Select(); // for motif
  544. #if 0
  545.     fAlnView->GetReadyToShow(); // do before others ...
  546.     if (fAlnIndex) fAlnIndex->GetReadyToShow();
  547.     fAlnITitle->GetReadyToShow();
  548.  
  549.     if (0) {
  550.             // try auto-sizing aln view to data
  551.             //long ns= fSeqList->GetSize();
  552.     long ht = 0, nrow= fAlnView->GetMaxRows();
  553.     if (nrow > 2) {
  554.         ht = 80 + fAlnITitle->GetItemHeight(0);  
  555.         if (nrow > 25) nrow= 25; // max size?
  556.         for (long i= 0; i<nrow; i++) ht += fAlnView->GetItemHeight(i);
  557.         Nlm_RecT r;
  558.         GetPosition(r);
  559.         r.bottom = r.top + ht;
  560.         SetPosition(r);
  561.         }
  562.     }
  563.     this->CalcWindowSize();
  564.  
  565. #else
  566.     this->CalcWindowSize();
  567.     fAlnView->GetReadyToShow(); // do before others ...
  568.     if (fAlnIndex) fAlnIndex->GetReadyToShow();
  569.     fAlnITitle->GetReadyToShow();
  570.     //fAlnHIndex->GetReadyToShow();
  571.  
  572. #endif
  573.  
  574.     fAlnView->SetViewColor(fgUseColor);
  575.     fAlnView->SetViewMode(fgViewMode);
  576.     if (selectone && fAlnIndex) fAlnIndex->SelectCells(0,0);
  577.     fSeqMeter->Show();  
  578.     
  579.     DWindow::Open();
  580. }
  581.  
  582.  
  583. void DSeqDoc::Activate()
  584. {
  585.     if (fAlnView && fAlnView->fEditSeq && fAlnView->fEditSeq->fVisible) 
  586.         fAlnView->fEditSeq->selectAction();
  587.     gCursor->arrow();
  588.     DWindow::Activate();
  589. }
  590.  
  591. void DSeqDoc::Deactivate()
  592. {
  593.     if (fAlnView && fAlnView->fEditSeq) fAlnView->fEditSeq->deselectAction();
  594.     DWindow::Deactivate();
  595. }
  596.  
  597. void DSeqDoc::Print()
  598. {
  599.     // fix this to print more of window !
  600.     //fAlnView->Print();
  601.     
  602.     // or do prettyprint...
  603.     MakeSeqPrint( false);
  604.     fPrintDoc->PrintDoc();
  605. }
  606.  
  607. Boolean DSeqDoc::IsMyTask(DTask* theTask) 
  608. {
  609. #if 0
  610.     if (theTask->fKind == kSeqDoc) {
  611.         ProcessTask( theTask);
  612.         return true;
  613.         }
  614.     else 
  615. #endif
  616.         return DWindow::IsMyTask(theTask);
  617. }
  618.  
  619. void DSeqDoc::ProcessTask(DTask* theTask) 
  620. {
  621.     if (theTask->fNumber == -987654) {
  622.  
  623.         }
  624.     else {
  625.         DWindow::ProcessTask(theTask);
  626.         }
  627. }
  628.  
  629.  
  630. void DSeqDoc::SortView(DSeqList::Sorts sortorder)
  631. {
  632.     fSeqList->SortList(sortorder);
  633.     fAlnView->Invalidate();
  634.     if (fAlnIndex) fAlnIndex->Invalidate();
  635. }
  636.  
  637.  
  638. void DSeqDoc::AddSeqToList(DSequence* item)
  639. {
  640.     fAlnView->addToAlnList( item);
  641.     //fAlnView->SelectCells( fAlnView->GetMaxRows()-1, fAlnView->GetMaxCols());
  642.     if (fAlnIndex) fAlnIndex->SelectCells( fAlnView->GetMaxRows()-1, 0); 
  643. #if 0
  644.     this->Changed( 1, this);
  645. #endif
  646. }    
  647.  
  648.  
  649.  
  650. void DSeqDoc::AddNewSeqToList()
  651. {
  652.     DSequence* aSeq = new DSequence(); 
  653.     AddSeqToList( aSeq);
  654. }
  655.  
  656.  
  657. void DSeqDoc::FirstSelection( DSequence*& aSeq, long& start, long& nbases)
  658. {    
  659.     long            left, right, top, bottom, atRow;
  660.  
  661.     start= nbases= atRow= 0;
  662.     fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  663.     fAlnView->GetFirstSelectedCell(top,left);
  664.     fAlnView->GetLastSelectedCell(bottom,right);
  665.     
  666.     if (left !=  DTabSelection::kNoSelection && top !=  DTabSelection::kNoSelection 
  667.      && (left != right || top != bottom)) {
  668.       atRow = top;
  669.         start = left; // - 1;
  670.         nbases= right - start;  
  671.         }    
  672.     else if (fAlnIndex && fAlnIndex->IsSelected()) {
  673.         fAlnIndex->GetFirstSelectedCell(top,left);
  674.         fAlnIndex->GetLastSelectedCell(bottom,right);
  675.         atRow= top;
  676.         start= 0; 
  677.         nbases= 0; //key for all of seq
  678.         }    
  679.  
  680.     aSeq= fAlnView->SeqAt( atRow);
  681.     if (aSeq) {
  682.         aSeq->SetIndex(atRow);
  683.         aSeq->SetSelection( start, nbases);
  684.         }
  685. }
  686.  
  687. void DSeqDoc::AddSeqAtToList( long aRow, long start1, long nbases1, 
  688.                                                         DSeqList*& aSeqList, long& start, long& nbases)
  689. {
  690.     DSequence* aSeq= fAlnView->SeqAt( aRow); 
  691.     if (aSeq) {
  692.         aSeq->SetIndex( aRow);
  693.         aSeq->SetSelection( start1, nbases1);
  694.         aSeqList->InsertLast( aSeq);
  695.         start= Max(start,start1);
  696.         if (nbases==0) nbases= nbases1;
  697.         else nbases= Min(nbases,nbases1);
  698.         }
  699. }
  700.  
  701. void DSeqDoc::AddMaskedSeqToList( long aRow, short masklevel,
  702.                                     DSeqList*& aSeqList, long& start, long& nbases)
  703. {
  704.     DSequence* aSeq= fAlnView->SeqAt( aRow); 
  705.     if (aSeq && aSeq->MasksOk()) {
  706.         aSeq->ClearSelection();
  707.         aSeq= aSeq->CompressFromMask(masklevel);
  708.             // !! NOTE these Seqs are NEW -- delete when aSeqList is deleted
  709.         if (!aSeq) return;
  710.         if (aSeq->LengthF() == 0) { delete aSeq; return; }
  711.         aSeq->SetIndex( aRow);
  712.         aSeqList->InsertLast( aSeq);
  713.         aSeqList->fDeleteObjects= true;  // so NEW seqs are deleted
  714.         start= 0;
  715.         if (nbases==0) nbases= aSeq->LengthF();
  716.         else nbases= Min(nbases,aSeq->LengthF());
  717.         }
  718. }
  719.  
  720. #if 0
  721. enum  selectforms { 
  722.     kSeqSel = 1, kNoSeqSel = 0,
  723.     kMaskSel = 2, kNoMaskSel= 0,
  724.     kIndexSel = 4, kNoIndexSel = 0,
  725.     kAllIfNone = 8, kNoneIfNone = 0,
  726.     kEqualCount = 16, kUnequalCount = 0,
  727.     };
  728.   // kSeqSel+kMaskSel+kIndexSel+kAllIfNone+kEqualCount
  729. #endif
  730.   
  731. void DSeqDoc::GetSelection( long selectFlags, DSeqList*& aSeqList, long& start, long& nbases)
  732. // GetSelection(Boolean equalCount, Boolean allAtNoSelection,
  733. {
  734.     long            left, right, top, bottom, arow, acol;
  735.     long            start1, nbases1;
  736.     Boolean        equalCount, allAtNoSelection, useSeqSelection, useMasks;
  737.  
  738.     equalCount = (selectFlags & kEqualCount);
  739.     allAtNoSelection = (selectFlags & kAllIfNone);
  740.     useMasks = (selectFlags & kMaskSel);
  741.     useSeqSelection = (selectFlags & kSeqSel);
  742.     
  743.     fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  744.     start= 0; start1= 0;
  745.     nbases= 0; nbases1= 0;
  746.     aSeqList= new DSeqList();
  747.     useMasks= useMasks && (fAlnView->fMaskLevel > 0);
  748.     
  749.     fAlnView->GetFirstSelectedCell(top,left);
  750.     fAlnView->GetLastSelectedCell(bottom,right);
  751.     useSeqSelection = useSeqSelection && (!useMasks 
  752.             && left != DTabSelection::kNoSelection 
  753.             && top != DTabSelection::kNoSelection 
  754.              && (left != right || top != bottom));
  755.     
  756. #if 0
  757.         // THIS IS STILL BUGGY -- need to TEST & FIX
  758.     if (useMasks && fAlnView->IsMasked()) {
  759.         top= 0;
  760.         bottom= fSeqList->GetSize();
  761.         if (fAlnIndex && fAlnIndex->IsSelected()) {
  762.             fAlnIndex->GetFirstSelectedCell(top,left);
  763.             fAlnIndex->GetLastSelectedCell(bottom,right);
  764.             }
  765.         for (arow= top; arow<bottom; arow++)  
  766.             AddMaskedSeqToList( arow, fAlnView->fMaskLevel, 
  767.                                                         aSeqList, start, nbases);
  768.         }        
  769.     else 
  770. #endif
  771.  
  772.     if (useSeqSelection) {
  773.         for (arow= top; arow<bottom; arow++) {
  774.             if (equalCount) acol= left; else acol= 0; 
  775.             
  776.             while (acol<right && !fAlnView->IsSelected(arow,acol)) acol++;
  777.             start1= acol; // - 1;
  778.             if (equalCount)
  779.                 while (acol<right && fAlnView->IsSelected(arow,acol)) acol++;
  780.             else 
  781.                 while (fAlnView->IsSelected(arow,acol)) acol++;
  782.             nbases1= acol - start1;
  783.             
  784.             AddSeqAtToList( arow, start1, nbases1, aSeqList, start, nbases);
  785.             }
  786.         start = left; // - 1;
  787.         nbases= right - start;  
  788.         }    
  789.         
  790.     else if (fAlnIndex && fAlnIndex->IsSelected()) {
  791.         fAlnIndex->GetFirstSelectedCell(top,left);
  792.         fAlnIndex->GetLastSelectedCell(bottom,right);
  793.         for (arow= top; arow<bottom; arow++) 
  794.             AddSeqAtToList( arow, start1, nbases1, aSeqList, start, nbases);
  795.         }    
  796.         
  797.     else if (allAtNoSelection) {
  798.          for (arow= 0; arow<fSeqList->GetSize(); arow++) 
  799.             AddSeqAtToList( arow, start1, nbases1, aSeqList, start, nbases);
  800.         }
  801. }
  802.             
  803.  
  804. short DSeqDoc::SelectionToFile(Boolean AllatNoSelection, char* aFileName, short seqFormat)  
  805. {
  806.     long        start, nbases;
  807.     long        nseqs;
  808.     DSeqList *aSeqList;
  809.  
  810.     GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone, aSeqList, start, nbases);
  811.     nseqs= aSeqList->GetSize();
  812.     aSeqList->DoWrite( aFileName, seqFormat); 
  813.     delete aSeqList;
  814.     return nseqs;
  815. }
  816.  
  817.  
  818. void DSeqDoc::NotDirty()
  819. {
  820.     DSaveHandler::NotDirty();
  821.     long i, nseqs= fSeqList->GetSize();
  822.     for (i=0; i<nseqs; i++) {
  823.         fSeqList->SeqAt(i)->SetChanged(false);
  824.         }
  825.     if (fAlnIndex) fAlnIndex->Invalidate();
  826. }  
  827.  
  828.  
  829. void DSeqDoc::WriteTo(DFile* aFile)  
  830. {
  831.     short outformat= DSeqFile::kGenBank;
  832.     if (fFormatPop) outformat= fFormatPop->GetValue(); 
  833.     fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  834.     if (fSaveSelection) {
  835.         long        start, nbases;
  836.         DSeqList* aSeqList= NULL;
  837.         fSaveSelection= false;
  838.         GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone, aSeqList, start, nbases);
  839.         aSeqList->DoWrite( aFile, outformat);
  840.         delete aSeqList;
  841.         }    
  842.     else {
  843.         fSeqList->ClearSelections(); //! make sure we write all of seq from this call !?
  844.         fSeqList->DoWrite( aFile, outformat);
  845.         this->NotDirty();  
  846.         }
  847. }
  848.  
  849.  
  850.  
  851.  
  852.  
  853. Boolean DSeqDoc::ReadFrom(DFile* aFile, Boolean append)    // revise for iostreams
  854. {
  855.     if (!append) FreeData();   //?? or leave here for append...
  856.     gDefSeqName= this->GetTitle(gDefSeqNameStore,sizeof(gDefSeqNameStore));
  857.     fInFormat= DSeqFile::ReadSeqFile(aFile, fSeqList);  
  858.     return true; //?? (fInFormat != DSeqFile::kUnknown)
  859. }
  860.  
  861.  
  862.  
  863.  
  864. void DSeqDoc::OpenSeqedWindow(DSequence* aSeq)
  865. {
  866.     char* title= StrDup( aSeq->Name());
  867.     StrExtendCat( &title, " Edit");
  868.     DSeqedWindow* edwin = new DSeqedWindow( DSeqedWindow::kId, gApplication, 
  869.                                                 this, aSeq, -5, -5, -50, -20, title);
  870.     MemFree( title);
  871.     edwin->Open(); 
  872.     edwin->Select();     
  873. }
  874.  
  875.  
  876.  
  877. void DSeqDoc::EditSeqs() 
  878. {
  879.     DSequence* aSeq= fAlnView->SelectedSequence();  
  880.     if (aSeq) this->OpenSeqedWindow(aSeq);
  881. }
  882.  
  883.  
  884.  
  885.  
  886. void DSeqDoc::ToTextDoc()
  887. {
  888.     DTempFile* tmpFile= new DTempFile();
  889.     this->WriteTo(tmpFile);
  890.     DRichTextDoc* doc= new DRichTextDoc(0,  true, gTextFont);
  891.     doc->Open(tmpFile);
  892.     delete tmpFile;
  893. }
  894.  
  895.  
  896. void DSeqDoc::MakeSeqPrint(Boolean doREMap)
  897. {
  898.     long        firstbase, nbases;
  899.     DSeqList* aSeqList= NULL;
  900.     fSaveSelection= false;
  901.     DWindow    * aDoc;
  902.     
  903.     GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone, //+kEqualCount??
  904.                                 aSeqList, firstbase, nbases);
  905.     if (!aSeqList)
  906.         return;
  907.     else if (doREMap) {
  908.         aDoc = new DREMapPrintDoc( DSeqPrintDoc::kSeqPrintDoc, this, aSeqList, firstbase, nbases);
  909.         aDoc->Open();  
  910.         }
  911.     else if (aSeqList->GetSize() > 1) {
  912.         aDoc = new DAlnPrintDoc( DSeqPrintDoc::kSeqPrintDoc, this, aSeqList, firstbase, nbases);
  913.         aDoc->Open();  
  914.         }        
  915.     else {
  916.         aDoc = new DSeqPrintDoc( DSeqPrintDoc::kSeqPrintDoc, this, aSeqList, firstbase, nbases);
  917.         aDoc->Open();  
  918.         }
  919.     fPrintDoc= aDoc;
  920. }
  921.  
  922. void DSeqDoc::MakeAlnPrint()
  923. {
  924.     MakeSeqPrint(false);
  925. }
  926.  
  927.  
  928.  
  929. void DSeqDoc::SetUpMenu(short menuId, DMenu*& aMenu) 
  930. {
  931.     //DWindow::SetUpMenu(menuId, aMenu);
  932.  
  933.     if (menuId == kSeqMenu) {
  934.         if (!aMenu) aMenu = gApplication->NewMenu( menuId, "Sequence");
  935.         //aMenu->AddItem( cSeqPrefs, "Seq Prefs...");    
  936.     
  937.         aMenu->AddItem( cNewSeq, "New sequence");
  938.         aMenu->AddItem( cEditSeq, "Edit seq...");
  939.      
  940.         DMenu* vMenu= new DSubMenu( kViewKindMenu, aMenu, "View seqs");
  941.         vMenu->AddItem( kViewByDefault, "default");
  942.         vMenu->AddItem( kViewByDate, "by Date");
  943.         vMenu->AddItem( kViewBySize, "by Size");
  944.         vMenu->AddItem( kViewByName, "by Name");
  945.         vMenu->AddItem( kViewByKind, "by Kind");
  946.         vMenu->AddItem( kViewAsText, "as Text");
  947.  
  948.         DMenu* mMenu= new DSubMenu( kSeqMaskMenu, aMenu, "Seq mask");
  949.         mMenu->AddItem(cMaskSelAll,"Mask all",false, true);
  950.         mMenu->AddItem(cMaskInvert,"Invert mask",false, true);
  951.         mMenu->AddItem(cMaskClear,"Clear mask",false, true);
  952.         mMenu->AddSeparator();
  953.  
  954.         mMenu->AddItem(cSel2Mask,"Selection to Mask",false, true);
  955.         mMenu->AddItem(cMask2Sel,"Mask to Selection",false, true);
  956.         mMenu->AddItem(cMaskOrSel,"Mask OR Selection",false, true);
  957.         mMenu->AddItem(cMaskAndSel,"Mask AND Selection",false, true);
  958.         //gViewCentral->DisableView(cMask2Sel);
  959.         //gViewCentral->DisableView(cMaskOrSel);
  960.         //gViewCentral->DisableView(cMaskAndSel);
  961.         mMenu->AddSeparator();
  962.         mMenu->AddItem(cMaskCompress, "Compress by mask");
  963.         mMenu->AddItem(cMaskSelCommon,"Mask consensus",false, true);
  964.         mMenu->AddItem(cMaskSelORF,"Mask ORFs",false, true);
  965.         mMenu->AddItem(cMaskReplicate,"Replicate mask to all seqs",false, true);
  966.  
  967.         aMenu->AddSeparator();
  968.         aMenu->AddItem( cRevSeq, "Reverse");
  969.         aMenu->AddItem( cCompSeq, "Complement");
  970.         aMenu->AddItem( cRevCompSeq, "Rev-Compl");
  971.         aMenu->AddItem( cDna2Rna, "Dna->Rna");
  972.         aMenu->AddItem( cRna2Dna, "Rna->Dna");
  973.         aMenu->AddItem( cToUpper, "UPPER Case");
  974.         aMenu->AddItem( cToLower, "lower Case");
  975.  
  976.         aMenu->AddItem( cDegap, "Degap");
  977.         aMenu->AddItem( cLockIndels, "Lock indels");
  978.         aMenu->AddItem( cUnlockIndels, "Unlock indels");
  979.         aMenu->AddItem( cConsensus, "Consensus");
  980.         aMenu->AddItem( cTranslate, "Translate");
  981.         aMenu->AddItem( cDistance, "Distance");
  982.         //aMenu->AddItem( cSimilarity, "Similarity");
  983.  
  984.         aMenu->AddSeparator();
  985.         aMenu->AddItem( cPrettyPrint, "Pretty Print...");
  986.         aMenu->AddItem( cREMap, "Restriction map...");
  987.         aMenu->AddItem( cDotPlot, "Dot plot...");
  988.         gViewCentral->DisableView(cDotPlot);
  989.  
  990.         aMenu->AddSeparator();        
  991.         aMenu->AddItem( cNAcodes, "Nucleic codes...");
  992.         aMenu->AddItem( cAAcodes, "Amino codes...");
  993.          }
  994.      
  995.     else if (menuId == DApplication::cEditMenu) {
  996.         aMenu->AddSeparator();
  997.         aMenu->AddItem(DApplication::kFind,"Find.../F",false, true);
  998.         aMenu->AddItem(DApplication::kFindAgain,"Find again/G",false, true);
  999.         aMenu->AddItem(cFindORF,"Find ORF/R",false, true);
  1000.  
  1001.         }
  1002.         
  1003.      else if (menuId == kInternetMenu) {
  1004.         if (!aMenu) aMenu = gApplication->NewMenu( menuId, "Internet");
  1005.         aMenu->AddItem( 0, "[Mail servers]");
  1006.         aMenu->AddItem( cNCBIfetch, "NCBI fetch...");
  1007.         aMenu->AddItem( cNCBIblast, "NCBI BLAST search...");
  1008.         aMenu->AddItem( cEMBLfetch, "EMBL fetch...");
  1009.         aMenu->AddItem( cEMBLquicks, "EMBL Quicksearch...");
  1010.         aMenu->AddItem( cEMBLfasta, "EMBL FastA search...");
  1011.         aMenu->AddItem( cEMBLblitz, "EMBL Blitz search...");
  1012.         aMenu->AddItem( cFHCRCfetch, "FHCRC fetch...");
  1013.         aMenu->AddItem( cFHCRCblocks, "FHCRC Blocks search...");
  1014.         aMenu->AddItem( cGeneidSearch, "Geneid search...");
  1015.         aMenu->AddItem( cGrailSearch, "Grail search...");
  1016.         //aMenu->AddItem( cGenmarkSearch, "Genmark search...");
  1017.         //aMenu->AddItem( cPythiaSearch, "Pythia search...");
  1018.          //aMenu->AddSeparator();    
  1019.          }                            
  1020.  
  1021. }
  1022.  
  1023.  
  1024.  
  1025.  
  1026. char* DSeqDoc::GetTitle(char* title, ulong maxsize) 
  1027.     if (fSaveSelection) {
  1028.         if (title==NULL) title= (char*) MemNew(maxsize); 
  1029.         DWindow::GetTitle( title, maxsize); 
  1030. #ifdef OS_DOS
  1031.         DFileManager::ReplaceSuffix( title, maxsize, ".sel");
  1032. #else
  1033.         StrNCat( title,".part", maxsize); 
  1034. #endif
  1035.         return title;
  1036.         }
  1037.     else if (fDocTitle && *fDocTitle) {
  1038.         if (title==NULL) {
  1039.             maxsize= Min(maxsize,StrLen(fDocTitle)+1);
  1040.             title= (char*) MemNew(maxsize); 
  1041.             }
  1042.         StrNCpy(title,fDocTitle,maxsize);
  1043.         return title;
  1044.         }
  1045.     else {
  1046.         DWindow::GetTitle( title, maxsize);
  1047.         //if (StringCmp(title,kUntitled)==0) *title= 0; // set to null so saveas is called
  1048.         return title;
  1049.         }
  1050. }
  1051.  
  1052. void DSeqDoc::FindORF()
  1053. {
  1054.     long    start, stop;
  1055.     DSequence* aSeq;
  1056.     long            selrow= 0;
  1057.     Nlm_RecT    selr;
  1058.     
  1059.     aSeq= fAlnView->SelectedSequence(selrow);
  1060.     if (aSeq) {
  1061.         selr= fAlnView->GetSelRect();
  1062.         if (selr.right > selr.left+1) selr.left++; // !? offset sel start so we can repeat call
  1063.         selr.top= selrow;
  1064.         selr.bottom= selrow+1; 
  1065.         aSeq->SetSelection( selr.left, 0);
  1066.         aSeq->SearchORF( start, stop);
  1067.         if (start>=0) {
  1068.             if (stop<start) stop= aSeq->LengthF(); //??
  1069.             aSeq->SetSelection( start, stop - start);
  1070.             selr.left= start;
  1071.             selr.right= stop;
  1072.             fAlnView->SelectCells( selr);
  1073.             if (fAlnView->fEditSeq && fAlnView->fEditRow == selrow)
  1074.                 fAlnView->fEditSeq->Select(start, stop - start);
  1075.             }
  1076.         } 
  1077. }
  1078.  
  1079.  
  1080. void DSeqDoc::MakeConsensus()
  1081. {
  1082.     long    firstBase = 0, nBases = 0;
  1083.     DSeqList * aSeqList= NULL;
  1084.     DSequence* cons;
  1085.     
  1086.     GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone+kEqualCount, 
  1087.                                 aSeqList, firstBase, nBases);
  1088.     if (aSeqList) {
  1089.         cons= fSeqList->Consensus();
  1090.         if (cons) {
  1091.             long aRow, totalRows;
  1092.             Nlm_RecT    r;
  1093.             aRow= fSeqList->GetIdentityItemNo( cons);
  1094.             totalRows= fSeqList->GetSize();
  1095.             fSeqList->Delete(cons);
  1096.             delete cons;
  1097.             fAlnView->GetRowRect( aRow, r, totalRows-aRow+1);
  1098.             fAlnView->InvalRect( r);
  1099.             if (fAlnIndex) {
  1100.                 fAlnIndex->GetRowRect( aRow, r, totalRows-aRow+1);
  1101.                 fAlnIndex->InvalRect( r);
  1102.                 }
  1103.             fAlnView->UpdateSize();   
  1104.             }
  1105.             
  1106.         aSeqList->MakeConsensus();
  1107.         cons= aSeqList->Consensus();
  1108.         if (cons) {
  1109.             AddSeqToList( cons);
  1110.             //if (fAlnIndex) fAlnIndex->SelectCells( fSeqList->ConsensusRow(), 0); 
  1111.             //fAlnIndex->ScrollSelectionIntoView(TRUE);
  1112.             }
  1113.         delete aSeqList;
  1114.         }
  1115. }
  1116.  
  1117. void DSeqDoc::DistanceMatrix(short form)
  1118. {
  1119.     long    firstBase = 0, nBases = 0;
  1120.     DSeqList * aSeqList= NULL;
  1121.     short correction= fgDistCor; //DSeqList::DCnone; //DCnone, DCjukes, DColsen 
  1122.     char  title[128];
  1123.     
  1124.     this->GetTitle(title, sizeof(title));
  1125.     
  1126.     GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone+kEqualCount, 
  1127.                                 aSeqList, firstBase, nBases);
  1128.     if (aSeqList) {            
  1129.         char* dmat = NULL;
  1130.         switch (form) {
  1131.             case 1: 
  1132.                 DFileManager::ReplaceSuffix(title, sizeof(title), ".Similarities");
  1133.                 dmat= aSeqList->Similarities(correction);  
  1134.                 break;
  1135.             case 0: 
  1136.             default:
  1137.                 DFileManager::ReplaceSuffix(title, sizeof(title), ".Distances");
  1138.                 dmat= aSeqList->Distances(correction); 
  1139.                 break;
  1140.             }
  1141.             
  1142.         if (dmat) {
  1143.             DRichTextDoc* doc= new DRichTextDoc();
  1144.             doc->fRichView->fDocFormat= DRichHandler::kTextformat;
  1145.             doc->SetTitle(title);
  1146.             doc->OpenText( dmat);
  1147.             }
  1148.         delete aSeqList;
  1149.         }
  1150. }
  1151.  
  1152.  
  1153. #if NETBLAST
  1154. extern "C" short CallNCBIBlastService( char* blastProgram, char* blastDatabase, char* blastOptions, 
  1155.                                                      char* fastaInputFile, char* outputFile );
  1156.  
  1157. void DSeqDoc::BLASTdialog()
  1158. {
  1159.     char* blastProgram = "blastn";
  1160.     char* blastDatabase = "nr";
  1161.     char* blastOptions = NULL;
  1162.     char* fastaInputFile = "spup-blast-input.fasta";
  1163.     char* outputFile = "spup-blast-result.text";
  1164.     char    instore[128];
  1165.     char  outstore[128];
  1166.     DFile aFile;
  1167.             
  1168.     fastaInputFile= DFileManager::TempFilename( instore);
  1169.     outputFile= DFileManager::TempFilename( outstore);
  1170.  
  1171.     short nseq= SelectionToFile( false, fastaInputFile, DSeqFile::kPearson);
  1172.     if (nseq) {
  1173.         short err= CallNCBIBlastService(blastProgram, blastDatabase, blastOptions, 
  1174.                                                 fastaInputFile, outputFile);
  1175.  
  1176.         if (err) {
  1177.             Message(MSG_OK,"DSeqDoc::CallNCBIBlastService error %d", err);
  1178.             }
  1179.         else {
  1180.             DRichTextDoc* doc= new DRichTextDoc(0,  true, gTextFont);
  1181.             doc->Open(outputFile);
  1182.             aFile.Initialize(outputFile);
  1183.             aFile.Delete();
  1184.             }
  1185.          }
  1186.     aFile.Initialize(fastaInputFile);
  1187.     aFile.Delete();
  1188. }
  1189. #endif
  1190.  
  1191.  
  1192. Boolean DSeqDoc::DoMenuTask(long tasknum, DTask* theTask)
  1193. {
  1194.     DWindow* win = NULL;
  1195.     
  1196.     switch (tasknum) {
  1197.     
  1198.         case cNewSeq        : 
  1199.             AddNewSeqToList(); 
  1200.             //fall into EditSeqs
  1201.         case cEditSeq: 
  1202.             EditSeqs(); 
  1203.             return true;
  1204.  
  1205.         case cSaveSel:
  1206.             fSaveSelection= true;
  1207.             gApplication->DoMenuTask( DApplication::kSaveACopy, NULL);
  1208.             fSaveSelection= false;
  1209.             return true;
  1210.  
  1211.         case cRevert:
  1212.             Revert();
  1213.             return true;
  1214.             
  1215.         case cFindORF: 
  1216.             FindORF();
  1217.             return true;
  1218.             
  1219.         case cConsensus:
  1220.             MakeConsensus();
  1221.             return true;
  1222.  
  1223.         case cDistance:
  1224.             DistanceMatrix(fgMatKind);
  1225.             return true;
  1226.  
  1227.         case cSimilarity:
  1228.             DistanceMatrix(1);
  1229.             return true;
  1230.                     
  1231.     
  1232.         case cRevSeq:
  1233.         case cCompSeq:
  1234.         case cRevCompSeq:
  1235.         case cDna2Rna: 
  1236.         case cRna2Dna:
  1237.         case cToUpper: 
  1238.         case cToLower:
  1239.         case cDegap:
  1240.         case cMaskCompress:
  1241.         case cLockIndels:
  1242.         case cUnlockIndels:
  1243.         case cTranslate:
  1244.             {
  1245.             long    firstBase = 0, nBases = 0;
  1246.             DSeqList * aSeqList= NULL;
  1247.             DSeqChangeCmd *    cmd = NULL;  
  1248.             
  1249.             //fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  1250.             long flags;
  1251.             flags= kIndexSel+kAllIfNone; //+kEqualCount??
  1252.             if (tasknum != cMaskCompress) flags |= kMaskSel|kSeqSel;
  1253.             GetSelection( flags, aSeqList, firstBase, nBases);
  1254.             switch (tasknum) {
  1255.               case cRevSeq        : cmd= new DSeqReverseCmd( this, fAlnView, aSeqList);  break;
  1256.                 case cCompSeq        : cmd= new DSeqComplementCmd( this, fAlnView,aSeqList); break;
  1257.                 case cRevCompSeq: cmd= new DSeqRevComplCmd( this, fAlnView,aSeqList); break;
  1258.                 case cDna2Rna        : cmd= new DSeqDna2RnaCmd( this, fAlnView,aSeqList); break;
  1259.                 case cRna2Dna        :    cmd= new DSeqRna2DnaCmd( this, fAlnView,aSeqList); break;
  1260.                 case cToUpper        :    cmd= new DSeqUppercaseCmd( this, fAlnView,aSeqList); break;
  1261.                 case cToLower        :    cmd= new DSeqLowercaseCmd( this,fAlnView, aSeqList); break;
  1262.                 case cDegap            :    cmd= new DSeqCompressCmd( this, fAlnView,aSeqList); break;
  1263.                 case cLockIndels:    cmd= new DSeqLockIndelsCmd( this, fAlnView,aSeqList); break;
  1264.                 case cUnlockIndels: cmd= new DSeqUnlockIndelsCmd( this, fAlnView,aSeqList); break;
  1265.                 case cTranslate    :    cmd= new DSeqTranslateCmd( this, fAlnView,aSeqList); break;
  1266.  
  1267.                 case cMaskCompress:    cmd= new DSeqCompressMaskCmd( this, fAlnView,aSeqList); break;
  1268.  
  1269.                 default:
  1270.                     delete aSeqList;
  1271.                     Message(MSG_OK,"DSeqDoc::method not ready.");
  1272.                     return true;
  1273.                 
  1274.               }
  1275.           if (cmd) {
  1276.               if (cmd->Initialize()) PostTask( cmd);
  1277.               else {
  1278.                   // use cmd.fTitle==GetTitle for ::method name !?
  1279.                   Message(MSG_OK,"DSeqDoc::method failed.");
  1280.                   delete cmd; 
  1281.                     }
  1282.               }
  1283.             return true;    
  1284.             }
  1285.  
  1286.         case cPrettyPrint:  MakeSeqPrint( false); return true; //MakeAlnPrint();
  1287.         case cREMap:                MakeSeqPrint( true); return true;
  1288.         case cNAcodes:            NucCodesPicture(); return true;
  1289.         case cAAcodes:            AminoCodesPicture(); return true;
  1290.         case cDotPlot:            //MakeDottyPlot(); return true;
  1291.             Message(MSG_OK,"DSeqDoc::method not ready.");
  1292.             return true;
  1293.  
  1294. #if NETBLAST
  1295.         case cNCBIblast:
  1296.             {
  1297.             char* blastProgram = "blastn";
  1298.             char* blastDatabase = "nr";
  1299.             char* blastOptions = NULL;
  1300.             char* fastaInputFile = "spup-blast-input.fasta";
  1301.             char* outputFile = "spup-blast-result.text";
  1302.             char    instore[128];
  1303.             char  outstore[128];
  1304.             DFile aFile;
  1305.             
  1306.             fastaInputFile= DFileManager::TempFilename( instore);
  1307.             outputFile= DFileManager::TempFilename( outstore);
  1308.  
  1309.             short nseq= SelectionToFile( false, fastaInputFile, DSeqFile::kPearson);
  1310.             if (nseq) {
  1311.                 short err= CallNCBIBlastService(blastProgram, blastDatabase, blastOptions, 
  1312.                                                         fastaInputFile, outputFile);
  1313.  
  1314.                 if (err) {
  1315.                     Message(MSG_OK,"DSeqDoc::CallNCBIBlastService error %d", err);
  1316.                     }
  1317.                 else {
  1318.                     DRichTextDoc* doc= new DRichTextDoc(0,  true, gTextFont);
  1319.                     doc->Open(outputFile);
  1320.                     aFile.Initialize(outputFile);
  1321.                     aFile.Delete();
  1322.                     }
  1323.                  }
  1324.             aFile.Initialize(fastaInputFile);
  1325.             aFile.Delete();
  1326.             }
  1327.             return true;
  1328.             
  1329. #else        
  1330.         case cNCBIblast:
  1331. #endif
  1332.     
  1333.         case cGrailSearch:
  1334.         case cGeneidSearch:
  1335.       case cFHCRCblocks:
  1336.       case cEMBLfasta:
  1337.       case cEMBLblitz:
  1338.       case cEMBLquicks:
  1339.             {
  1340.             DSequence* aSeq= fAlnView->SelectedSequence();
  1341.             if (aSeq) {
  1342.                 switch (tasknum) {
  1343.                     case cNCBIblast: win= new DNCBIBlast(0, this, aSeq); break;
  1344.                      case cEMBLblitz: win= new DEMBLBlitz(0, this, aSeq); break;
  1345.                      case cEMBLfasta: win= new DEMBLFasta(0, this, aSeq); break;
  1346.                      case cEMBLquicks: win= new DEMBLQuicks(0, this, aSeq); break;
  1347.                      case cFHCRCblocks: win= new DFHCRCblocks(0, this, aSeq); break;
  1348.                      case cGeneidSearch: win= new DUWFGeneID(0, this, aSeq); break;
  1349.                      case cGrailSearch: win= new DORNLGrail(0, this, aSeq); break;
  1350.                     }
  1351.                 if (win) win->Open();
  1352.                 }
  1353.             return true;
  1354.             }
  1355.  
  1356.       case cFHCRCfetch:
  1357.         case cEMBLfetch:
  1358.         case cNCBIfetch:
  1359.             switch (tasknum) {
  1360.                 case cNCBIfetch: win= new DNCBIFetch(0, this); break;
  1361.                  case cEMBLfetch: win= new DEMBLFetch(0, this); break;
  1362.                  case cFHCRCfetch: win= new DFHCRCfetch(0, this); break;
  1363.                 }
  1364.             if (win) win->Open();
  1365.             return true;
  1366.                         
  1367.         case cGenmarkSearch:
  1368.         case cPythiaSearch:
  1369.             Message(MSG_OK,"DSeqDoc::method not ready.");
  1370.             return true;
  1371.  
  1372.  
  1373.         default: 
  1374.             return DTaskMaster::DoMenuTask(tasknum, theTask);
  1375.         }
  1376.  
  1377. }
  1378.  
  1379.  
  1380. Boolean DSeqDoc::IsMyAction(DTaskMaster* action) 
  1381. {
  1382.     enum appMenus { kInternetMenu = 100, kPrefsMenu, kOpenMenu };  // HACK ! from SeqPup.cpp
  1383.     long  menuid= 0, menuitem = action->Id();
  1384.     if (action->fSuperior) menuid = action->fSuperior->Id();
  1385.     
  1386.     if ( menuid == DApplication::cEditMenu
  1387.         && HasEditText() && fEditText == fAlnView->fEditSeq ) {
  1388.         if (fEditText->IsMyAction( action)) return true;
  1389.         }
  1390.         
  1391.     //if (menuid == DApplication::cEditMenu)  
  1392.     
  1393.     if ((menuid == kInternetMenu
  1394.         ||menuid == DSeqApps::kChildMenu)
  1395.      && (menuitem >= DSeqApps::kChildMenuBaseID 
  1396.          && menuitem <= DSeqApps::kChildMenuBaseID + 50) // +50 = HACK !! need better way to distinguish menu items !
  1397.     ) goto caseChildMenu;
  1398.         
  1399.     switch (menuitem) {
  1400.     
  1401.         case DApplication::kCut:
  1402.         case DApplication::kCopy:
  1403.         case DApplication::kClear:
  1404.           {
  1405.             DAlnEditCommand* cmd= new DAlnEditCommand(this, menuitem);
  1406.             PostTask( cmd);
  1407.             return true;            
  1408.             }
  1409.         
  1410.         case DApplication::kPaste:
  1411.             {
  1412.           DView* clipview= (DView*) gClipboardMgr->fClipView; // GetClipView();
  1413.             if (clipview && clipview->fKind == DAlnView::kindAlnView) {
  1414.                 DAlnPasteCommand* cmd= new DAlnPasteCommand(this);
  1415.                 PostTask( cmd);
  1416.                 return true;            
  1417.                 }
  1418.             else if (HasEditText()) {  /* && clipview->fKind == OStype("TEXT") */
  1419.                 if (fEditText->IsMyAction(action)) return true;
  1420.                 }
  1421.             return true; //?? always handle this message?
  1422.             }
  1423.             
  1424.         case DApplication::kSelectAll:
  1425.             if (fAlnIndex) {
  1426.             Nlm_RecT r;
  1427.             Nlm_LoadRect(& r, 0, 0, 1/*fAlnIndex->GetMaxCols()*/, fAlnIndex->GetMaxRows());
  1428.             fAlnIndex->SelectCells( r);
  1429.             return true;            
  1430.             }
  1431.  
  1432.         }
  1433.  
  1434.     if (menuid == kViewKindMenu)  
  1435.      switch (menuitem) {
  1436.             case kViewByDefault:
  1437.                     this->SortView(DSeqList::kSortByItem); return true;
  1438.             case kViewByDate:
  1439.                     this->SortView(DSeqList::kSortByDate); return true;
  1440.             case kViewBySize:
  1441.                     this->SortView(DSeqList::kSortBySize); return true;
  1442.             case kViewByName:
  1443.                     this->SortView(DSeqList::kSortByName); return true;
  1444.             case kViewByKind:
  1445.                     this->SortView(DSeqList::kSortByKind); return true;
  1446.             case kViewAsText:
  1447.                     this->ToTextDoc(); return true;
  1448.             default: return true;
  1449.             }
  1450.  
  1451.     else if (menuid == kSeqMaskMenu && fAlnView->fMaskLevel>0) {
  1452.         if (menuitem == cMaskCompress) 
  1453.             return DoMenuTask(menuitem, NULL);
  1454.         else
  1455.             return fAlnView->MaskCommand( menuitem);
  1456.         }
  1457.  
  1458.     else if (menuid == DSeqApps::kChildMenu) {
  1459.     caseChildMenu:
  1460.         switch (menuitem) {
  1461. #if 0
  1462.             case cAddChildCmd:
  1463.             case cRemoveChildCmd:  
  1464.                 DoMenuTask(menuitem);
  1465.                 break;
  1466. #endif    
  1467.             default:
  1468.                 {
  1469.                 long    firstBase = 0, nBases = 0;
  1470.                 DSeqList * aSeqList= NULL;
  1471.                 //fAlnView->DeInstallEditSeq(); //?? reinstall after ?
  1472.                 GetSelection( kSeqSel+kMaskSel+kIndexSel+kAllIfNone, //+kEqualCount ??
  1473.                                 aSeqList, firstBase, nBases);
  1474.                 DSeqApps::CallChildApp( menuitem, aSeqList);
  1475.                 if (aSeqList) delete aSeqList;
  1476.                 }
  1477.                 break;
  1478.             }
  1479.         }
  1480.         
  1481.     else if (menuitem == kColorPopup) {
  1482.         fgUseColor= fColorPop->GetValue();
  1483.         fAlnView->SetViewColor( fgUseColor);
  1484.         //fAlnView->Invalidate();
  1485.       return true;
  1486.         }
  1487.  
  1488.     else if (menuitem == kLockButHit) {
  1489.         fAlnView->SetTextLock( ((DView*)action)->GetStatus());
  1490.       return true;
  1491.       }
  1492.  
  1493.     else if (menuitem == kModePopup) {
  1494.         short mode= fModePop->GetValue();  
  1495.         fAlnView->SetViewMode( mode);
  1496.       return true;
  1497.       }
  1498.     
  1499.     else 
  1500.         return DoMenuTask(menuitem, NULL);
  1501.         
  1502.     return false;
  1503. }
  1504.             
  1505.  
  1506.  
  1507.  
  1508.